package work.xiaohong.jijin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import work.xiaohong.jijin.entity.Free;
import work.xiaohong.jijin.service.IFreeService;
import work.xiaohong.model.Result;
import work.xiaohong.model.StatusCode;

import java.util.List;

/**
 *  前端控制器
 * @author xiaohong
 * @since 2021-11-07 13:37:13
 */
@Tag(name = "可赎基金接口文档")
@RestController
@RequestMapping("/free")
public class FreeController {

   @Autowired
   private IFreeService freeService;


    /***
     * 条件分页查询
     * @param free
     * @param page
     * @param size
     * @return
     */
    @Operation(summary = "条件分页查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@RequestBody(required = false) @Parameter(name = "Free对象",required = true) Free free, @PathVariable(value="page")  int page, @PathVariable(value="size")  int size){
        Page<Free> pageInfo = freeService.page(new Page<Free>(page,size),new QueryWrapper<Free>(free));
        return new Result<Page>(true,StatusCode.OK.getCode(),"查询成功",pageInfo);
    }


    /***
     * 分页查询
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @Operation(summary = "分页查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@PathVariable(value="page")  int page, @PathVariable(value="size")  int size){
        //调用FreeService实现分页查询Free
        Page<Free> pageInfo = freeService.page(new Page<>(page,size));
        return new Result<Page>(true,StatusCode.OK.getCode(),"查询成功",pageInfo);
    }

    /***
     * 条件查询
     * @param free
     * @return
     */
    @Operation(summary = "条件查询",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PostMapping(value = "/search" )
    public Result<List<Free>> findList(@RequestBody(required = false) @Parameter(name = "Free对象",required = true) Free free){
        List<Free> list = freeService.list(new QueryWrapper<Free>(free));
        return new Result<List<Free>>(true,StatusCode.OK.getCode(),"查询成功",list);
    }


    /***
     * 根据Id删除free数据
     * @param id
     * @return
     */
    @Operation(summary = "根据ID删除Free",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameter(in = ParameterIn.PATH, name = "id", description = "删除对象ID", required = true)
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable(value="id") Integer id){
        if (freeService.removeById(id)){
            return new Result(true,StatusCode.OK.getCode(),"删除成功");
        }else {
            return new Result(true,StatusCode.ERROR.getCode(),"删除失败");
        }
    }



    /***
     * 根据Id批量删除free数据
     * @param ids
     * @return
     */
    @Operation(summary = "根据Id批量删除Free",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @DeleteMapping
    public Result deleteByIds(@RequestBody @Parameter(name = "需要删除的Free对象id集合",required = true) List<Integer> ids){
        if (freeService.removeByIds(ids)){
            return new Result(true,StatusCode.OK.getCode(),"批量删除成功");
        }else {
            return new Result(true,StatusCode.ERROR.getCode(),"批量删除失败");
        }
    }


    /***
     * 根据ID修改Free数据
     * @param free
     * @return
     */
    @Operation(summary = "根据ID修改Free",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PutMapping
    public Result updateById(@RequestBody @Parameter(name = "Free对象",required = true) Free free){
        if (freeService.updateById(free)){
            return new Result(true,StatusCode.OK.getCode(),"修改成功",free);
        }else {
            return new Result(true,StatusCode.ERROR.getCode(),"修改失败");
        }
    }


    /***
     * 新增Free数据
     * @param free
     * @return
     */
    @Operation(summary = "添加Free",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PostMapping
    public Result add(@RequestBody  @Parameter(name = "Free对象",required = true) Free free){
        if (freeService.save(free)){
            return new Result(true,StatusCode.OK.getCode(),"添加成功",free);
        }else {
            return new Result(true,StatusCode.ERROR.getCode(),"添加失败");
        }
    }

    /***
     * 根据ID查询Free数据
     * @param id
     * @return
     */
    @Operation(summary = "根据ID查询Free",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @Parameter(in = ParameterIn.PATH, name = "id", description = "查询对象ID", required = true)
    @GetMapping("/{id}")
    public Result<Free> findById(@PathVariable(value="id") Integer id){
        Free free = freeService.getById(id);
        return new Result<Free>(true,StatusCode.OK.getCode(),"查询成功",free);
    }

    /***
     * 根据Id批量查询Free数据
     * @param ids
     * @return
     */
    @Operation(summary = "根据Id批量查询Free数据",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @PostMapping("/ids")
    public Result<List<Free>> findByIds(@RequestBody @Parameter(name = "需要查询的Free对象id集合",required = true) List<Integer> ids){
        List<Free> freeList = freeService.listByIds(ids);
        return new Result<List<Free>>(true,StatusCode.OK.getCode(),"查询成功",freeList);
    }

    /***
     * 查询Free全部数据
     * @return
     */
    @Operation(summary = "查询所有Free",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
    @GetMapping
    public Result<List<Free>> findAll(){
        List<Free> list = freeService.list();
        return new Result<List<Free>>(true, StatusCode.OK.getCode(),"查询成功",list) ;
    }


}
